<template>
  <el-container class="layout-container">
    <!-- 左侧菜单 -->
    <el-aside :width="isCollapsed ? '60px' : '200px'" class="sidebar">
      <div class="el-aside__logo"></div>
      <el-menu
        :default-active="$route.path"
        active-text-color="#ffffff"
        background-color="transparent"
        text-color="#d9e0e7"
        router
        :collapse="isCollapsed"
        class="sidebar-menu"
      >
        <el-menu-item index="/index">
          <el-icon><Link /></el-icon>
          <span>返回首页</span>
        </el-menu-item>
        <el-menu-item index="/merchant/info">
          <el-icon><Shop /></el-icon>
          <span>商铺信息</span>
        </el-menu-item>
        <el-menu-item index="/merchant/orders">
          <el-icon><Promotion /></el-icon>
          <span>订单管理</span>
        </el-menu-item>
        <el-menu-item index="/merchant/products">
          <el-icon><Goods /></el-icon>
          <span>商品管理</span>
        </el-menu-item>
        <el-menu-item index="/merchant/promotions">
          <el-icon><Discount /></el-icon>
          <span>活动管理</span>
        </el-menu-item>
        <el-menu-item index="/merchant/refunds">
          <el-icon><Money /></el-icon>
          <span>退款管理</span>
        </el-menu-item>
        <el-menu-item index="/merchant/evaluations">
          <el-icon><StarFilled /></el-icon>
          <span>评价管理</span>
        </el-menu-item>
      </el-menu>
    </el-aside>
    <!-- 右侧主区域 -->
    <el-container>
      <!-- 头部区域 -->
      <el-header class="header">
        <div class="header-left">
          <el-icon class="toggle-icon" @click="isCollapsed = !isCollapsed">
            <Menu />
          </el-icon>
          <span class="welcome-text">欢迎：<strong>{{ userInfoStore.userInfo.username || '商户' }}</strong></span>
        </div>
        <el-dropdown placement="bottom-end" @command="handleCommand">
          <span class="el-dropdown__box">
            <el-avatar :src="avatar" class="avatar" />
            <el-icon><CaretBottom /></el-icon>
          </span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item command="info" :icon="Shop">商铺信息</el-dropdown-item>
              <el-dropdown-item command="logout" :icon="SwitchButton">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </el-header>
      <!-- 中间区域 -->
      <el-main class="main-content">
        <!-- 待审核提醒 -->
        <el-alert
          v-if="auditStatus === 0"
          type="warning"
          show-icon
          :closable="false"
          title="您的店铺正在审核中，部分功能暂不可用，请耐心等待审核通过"
          style="margin-bottom: 12px;"
        />
        <router-view />
      </el-main>
      <!-- 底部区域 -->
      <el-footer class="footer">商户后台管理系统 ©2024</el-footer>
    </el-container>
  </el-container>
</template>

<script setup>
import {
  CaretBottom,
  Discount,
  Goods,
  Menu,
  Money,
  Promotion,
  Shop,
  StarFilled,
  SwitchButton
} from '@element-plus/icons-vue'
import avatar from '@/assets/default.png'
import { getMerchantById } from '@/api/merchant'
import { useUserInfoStore } from '@/stores/userInfo'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { ref } from 'vue'

const userInfoStore = useUserInfoStore()
const router = useRouter()
const isCollapsed = ref(false)
const auditStatus = ref(null)  // 0=待审 1=通过 2=拒绝

const getUserInfo = () => {
  getMerchantById(userInfoStore.userInfo.id).then(res => {
    // 接口返回的数据一般在 res.data.data 中
    const merchant = res.data.data;
    if (!merchant) {
      router.push('/login');
      return;
    }

    auditStatus.value = merchant.auditStatus;

    if (merchant.auditStatus === 2) {
      // 审核被拒绝直接退回登录
      router.push('/login');
    }
  })
}
getUserInfo()

const handleCommand = (command) => {
  if (command === 'logout') {
    ElMessageBox.confirm('你确认要退出吗？', '温馨提示', {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning'
    }).then(async () => {
      userInfoStore.logout()
      ElMessage.success('退出成功')
      await router.push('/login')
    })
  } else {
    router.push('/merchant/' + command)
  }
}
</script>

<style lang="scss" scoped>
.layout-container {
  height: 100vh;
  overflow: hidden;

  .sidebar {
    background: var(--sidebar-bg);
    transition: width 0.2s ease;
    box-shadow: 2px 0 6px rgba(0, 0, 0, 0.1);

    &__logo {
      height: 48px;
      margin: 12px 0;
   //   background: url('@/assets/login_title.png') no-repeat center / 110px auto;
      transition: var(--transition);
    }

    .sidebar-menu {
      border-right: none;
      height: calc(100vh - 72px); /* 减去 logo 高度 */
      overflow: hidden;
    }
  }

  .header {
    background-color: var(--header-bg);
    display: flex;
    align-items: center;
    justify-content: space-between;
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.08);
    padding: 0 16px;
    height: 56px;

    .header-left {
      display: flex;
      align-items: center;
      gap: 10px;

      .toggle-icon {
        font-size: 20px;
        cursor: pointer;
        color: var(--text-secondary);
        transition: var(--transition);
        &:hover {
          color: var(--primary-color);
          transform: rotate(90deg);
        }
      }

      .welcome-text {
        font-size: 14px;
        color: var(--text-color);
        strong {
          font-weight: 600;
        }
      }
    }

    .el-dropdown__box {
      display: flex;
      align-items: center;
      gap: 6px;
      cursor: pointer;

      .avatar {
        width: 32px;
        height: 32px;
        transition: var(--transition);
        &:hover {
          transform: scale(1.05);
          border: 1px solid var(--primary-color);
        }
      }

      .el-icon {
        color: var(--text-secondary);
        font-size: 14px;
        transition: var(--transition);
        &:hover {
          color: var(--primary-color);
        }
      }
    }
  }

  .main-content {
    padding: 16px;
    height: calc(100vh - 112px); /* 减去 header 和 footer */
    overflow: hidden;
    display: flex;
    flex-direction: column;
  }

  .footer {
    height: 56px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 12px;
    color: var(--text-secondary);
    background-color: var(--header-bg);
    box-shadow: 0 -1px 4px rgba(0, 0, 0, 0.08);
  }
}
</style>